package com.mm.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;


/**
 * 拦截器类（统一拦截）
 *
 * @ClassName: BootInterceptor.java
 * @version: v1.0.0
 * @author: 杨朝湖
 * @date: 2019年4月16日 上午9:46:53
 */
public class BootInterceptor implements HandlerInterceptor {

    private static ThreadLocal<Long> threadLocal = new ThreadLocal<>();

    private static final String redisIp = "192.168.0.20";// 47.108.211.68

    private static final Logger logger = LoggerFactory.getLogger(BootInterceptor.class);

    /**
     * 拦截器前
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        Map<String, String[]> map = request.getParameterMap();
        /**
         * 对来自后台的请求统一进行日志处理
         */
        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        String uri = request.getRequestURI();
        logger.info(
                "---------------------------------------------------------------------------------------------------");
        map.forEach((k, v) -> {
            logger.info("请求参数-- " + k + ": " + v[0]);
        });
        logger.info("url--" + url);
        logger.info("method--" + method);
        logger.info("uri--" + uri);
        logger.info(
                "---------------------------------------------------------------------------------------------------");
        addHader(response, request);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
                           ModelAndView modelAndView) throws Exception {
    }

    /**
     * 拦截器后
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                Object o, Exception e) throws Exception {
    }

    /**
     * 跨域配置文件方法，导入返回Header
     *
     * @param response
     * @param request
     */
    public void addHader(HttpServletResponse response, HttpServletRequest request) {
        String[] allowedOrigins = {"http://192.168.0.35:8083", "http://192.168.0.35:8084"};
        String originHeader = request.getHeader("Origin") != null ? request.getHeader("Origin").replace(" ", "")
                : request.getHeader("Origin");
        if (Arrays.asList(allowedOrigins).contains(originHeader)) {
            response.addHeader("Access-Control-Allow-Origin", originHeader);
            response.addHeader("Access-Control-Allow-Credentials", "true");
            response.addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
            response.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
            response.addHeader("Access-Control-Max-Age", "3600");
        }
    }
}